package utils;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.util.Matrix;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Author hujian
 * @Date 2021/12/22 16:05
 * @Description pdfbox测试
 */
public class PdfUtils {

    public static void parse() {
        String path = "C:\\Users\\40138\\Desktop\\20230222解析\\3-fad8ad1a-a634-4c36-a821-5568d7f994a2.pdf";
        String parentPath = "C:\\Users\\40138\\Desktop\\download\\image2";
        for (int x = 0; x < 10; x++) {
            try (PDDocument document = PDDocument.load(new File(path));) {
                PDPageTree pages = document.getDocumentCatalog().getPages();
                int j = 0;
                long a = System.currentTimeMillis();
                for (int i = 0; i < pages.getCount(); i++) {
                    PDResources resources = pages.get(i).getResources();
                    Iterable<COSName> xObjectNames = resources.getXObjectNames();
                    if (null == xObjectNames) {
                        continue;
                    }
                    PDFTextStripper stripper = new PDFTextStripper();
                    stripper.setSortByPosition(false);// 排序
                    stripper.setStartPage(i);// 设置转换的开始页
                    stripper.setEndPage(i);// 设置转换的结束页
                    String content = stripper.getText(document);
//                System.out.println(content);
//                for (COSName cosName : xObjectNames) {
//                    if (resources.isImageXObject(cosName)) {
//                        PDImageXObject image = (PDImageXObject) resources.getXObject(cosName);
//                        int imageWidth = image.getWidth();
//                        int imageHeight = image.getHeight();
//                        System.out.println("\nImage [" + cosName.getName() + "]");
//                        BufferedImage bufferedImage = image.getImage();
//                        ImageIO.write(bufferedImage, "png", new File(parentPath + "\\" + "image" + "_" + (++j) + ".png"));
//                    }
//                }
                }
                long b = System.currentTimeMillis();
                System.out.println(b - a);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void parse1() {
        String path = "C:\\Users\\40138\\Desktop\\20230222解析\\3-fad8ad1a-a634-4c36-a821-5568d7f994a2.pdf";
        String parentPath = "C:\\Users\\40138\\Desktop\\download\\image2";
        for (int x = 0; x < 10; x++) {
            try (PDDocument document = PDDocument.load(new File(path));) {
                PDPageTree pages = document.getDocumentCatalog().getPages();
                long a = System.currentTimeMillis();
                PDFTextStripper stripper = new PDFTextStripper();
                stripper.setSortByPosition(false);// 排序
                stripper.setStartPage(1);// 设置转换的开始页
                stripper.setEndPage(pages.getCount());// 设置转换的结束页
                String content = stripper.getText(document);
                long b = System.currentTimeMillis();
//                System.out.println(content);
                System.out.println(b - a);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        parse1();
    }
}
